<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-09-29
 * Time: 13:48
 */

namespace backend\controllers;

use app\models\CrmHwzx;
use app\models\search\CrmHwzxSearch;

/**
 * 回访处理
 * Class CrmCallBackController
 * @package backend\controllers
 */
class CrmCallBackController extends CommonController
{
    //查询 列表
    public function actionList(){//查询
        $model = new CrmHwzxSearch();
        $lists = $model->search(\Yii::$app->request->post());
        $lists->query->asArray()->all();
        $limit = is_set($_POST,"limit");
        $offset = is_set($_POST,"limit")*(is_set($_POST,"page")-1);
        $lists->query->limit($limit)->offset($offset);
        return $this->actionLayData($lists->count,$lists->query->asArray()->all(),0,'',is_set($_POST,"page"));
    }

    // 指定分配
    public function actionZdfp(){
        $post = \Yii::$app->request->post();
        $khfp = $post['khfp'];
        $ygid = $post['dfpyg'][0]['id'];//要分配给的员工id
        $ts = $this->startT();//开启事务
        foreach ($khfp as $k=>$v){
            $t = CrmHwzx::findOne($v['id']);
            $res = $t->updateAttributes(['gsid'=>$ygid,'xgfpsj'=>time()]);//更新该条归属id
            if (!$res){
                $ts->rollBack();//遇见错误就回滚
                break;
            }
        }
        $ts->commit();//成功就提交
        return $this->dealFD("指定分配成功！",1);
    }

    // 平均分配
    public function actionPjfp(){
        $post = \Yii::$app->request->post();
        $khfp = $post['khfp'];//客户分配
        $dfpyg = $post['dfpyg'];//待分配员工
        $ts = $this->startT();//开启事务
        foreach ($khfp as $k=>$v){
            $t = CrmHwzx::findOne($v['id']);
            $kk = $k%count($dfpyg);//求余，拿到当前的索引
            $res = $t->updateAttributes(['gsid'=>is_set($dfpyg,$kk)['id'],'xgfpsj'=>time()]);//更新该条归属id
            if (!$res){
                $ts->rollBack();//遇见错误就回滚
                break;
            }
        }
        $ts->commit();//成功就提交
        return $this->dealFD("平均分配成功！",1);
    }

}